/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.allerj.gui.component;

import java.awt.Cursor;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

/**
 *
 * @author Alessandro Veracchi
 */
public class MyProgressBar extends JPanel
                                 implements ActionListener,
                                            PropertyChangeListener {
    private JLabel scanningTitle = new JLabel();
    private JProgressBar progressBar = new JProgressBar(0,100);
    private JPanel buttonBar = new JPanel();
    private JButton startButton = new JButton();
    private JPanel spacer = new JPanel();
    private String scanningTitleText = "title";
    private String scanText = "scan";
    private SwingWorker task;
    private JFrame mainJFrame;
    private ActionListener actionListener;

    public MyProgressBar initTask(SwingWorker task){
        this.task = task;
        return this;
    } 
    public MyProgressBar(String scanningTitleText, String scanText, JFrame mainJFrame) {
         super(new java.awt.GridBagLayout());
        this.scanningTitleText = scanningTitleText;
        this.scanText = scanText;
        this.mainJFrame = mainJFrame;
        showGui();
    }
    public MyProgressBar() {
        super(new java.awt.GridBagLayout());
        showGui();
    }
    public void setFont(Font font){
        super.setFont(font);
        if(this.scanningTitle!=null){
            this.scanningTitle.setFont(this.getFont());
        }
    }
    private void showGui(){
        scanningTitle.setText(scanningTitleText);       
        GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        add(scanningTitle, gridBagConstraints);

        progressBar.setMinimumSize(new java.awt.Dimension(200, 19));
        progressBar.setOpaque(true);
        progressBar.setPreferredSize(new java.awt.Dimension(200, 19));
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        add(progressBar, gridBagConstraints);

        buttonBar.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        startButton.setText(scanText);
        startButton.addActionListener(this);
        buttonBar.add(startButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        add(buttonBar, gridBagConstraints);

        spacer.setPreferredSize(new java.awt.Dimension(0, 0));

        javax.swing.GroupLayout spacerLayout = new javax.swing.GroupLayout(spacer);
        spacer.setLayout(spacerLayout);
        spacerLayout.setHorizontalGroup(
            spacerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 205, Short.MAX_VALUE)
        );
        spacerLayout.setVerticalGroup(
            spacerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 352, Short.MAX_VALUE)
        );

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        add(spacer, gridBagConstraints); 
    }
    public void actionPerformed(ActionEvent e) {
        if(actionListener!=null){
            actionListener.actionPerformed(e);
        } else if(task!=null){
            getStartButton().setEnabled(false);
            getMainJFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
            task.addPropertyChangeListener(this);
            task.execute();
        } else {
            throw new UnsupportedOperationException("Not inizilized task yet.");
        }
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (Integer) evt.getNewValue();
            getProgressBar().setValue(progress);
        }  
        //else {
        //    throw new UnsupportedOperationException("Not supported yet.");
        //}
    }

    public JProgressBar getProgressBar() {
        return progressBar;
    }

    public JButton getStartButton() {
        return startButton;
    }

    public String getScanningTitleText() {
        return scanningTitleText;
    }

    public void setScanningTitleText(String scanningTitleText) {
        this.scanningTitleText = scanningTitleText;
    }

    public String getScanText() {
        return scanText;
    }

    public void setScanText(String scanText) {
        this.scanText = scanText;
    }

    public SwingWorker getTask() {
        return task;
    }
    public void initActionListener(ActionListener actionListener){
        this.actionListener = actionListener;
    }

    public JFrame getMainJFrame() {
        return mainJFrame;
    }



}
